package org.bluesky.controller;

import org.bluesky.framework.ResponseResult;
import org.bluesky.framework.dto.AddRoleDto;
import org.bluesky.framework.dto.RoleDto;
import org.bluesky.framework.dto.UpdateRoleDto;
import org.bluesky.framework.entity.Role;
import org.bluesky.framework.service.RoleService;
import org.bluesky.framework.utils.BeanCopyUtils;
import org.bluesky.framework.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("/list")
    public ResponseResult getRoleList(Integer pageNum,Integer pageSize,String roleName,String status){
        return roleService.getRoleList(pageNum,pageSize,roleName,status);
    }

    @PutMapping("/changeStatus")
    public ResponseResult changeStatus(@RequestBody RoleDto roleDto){
        Role current_role = roleService.getById(roleDto.getRoleId());
        current_role.setStatus(roleDto.getStatus());
        roleService.updateById(current_role);
        return ResponseResult.okResult();
    }

    @PostMapping
    public ResponseResult addRole(@RequestBody AddRoleDto addRoleDto){
        return roleService.addRole(addRoleDto);
    }

    @GetMapping("/{id}")
    public ResponseResult getRole(@PathVariable("id")Long id){
        Role role = roleService.getById(id);
        RoleVo roleVo = BeanCopyUtils.copyBean(role, RoleVo.class);
        return ResponseResult.okResult(roleVo);
    }

    @PutMapping("")
    public ResponseResult updateRole(@RequestBody UpdateRoleDto updateRoleDto){
        return roleService.updateRoleById(updateRoleDto);
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteRoleById(@PathVariable("id")Long id){
        return roleService.deleteRoleById(id);
    }

    @GetMapping("/listAllRole")
    public ResponseResult listAllRole(){
        return roleService.rolesStatusNormal();
    }

}
